package com.itheima.web.manager;

import com.itheima.domain.db.UserFreezing;
import com.itheima.domain.db.UserInfo;
import com.itheima.service.db.FreezingService;
import com.itheima.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class FreezingManager {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Reference
    private UserInfoService userInfoService;
    @Reference
    private FreezingService freezingService;

    public void saveFreezing(UserFreezing userFreezing) {
        Integer userId = userFreezing.getUserId();
        userFreezing.setFreezing(new Date());
        Integer freezingRange = userFreezing.getFreezingRange();
        UserFreezing userFreezing1 = freezingService.findById(userId.longValue());
        if (userFreezing1 == null) {
            freezingService.saveFreezing(userFreezing);
        } else {
            userFreezing1.setFreezingRange(freezingRange);

            freezingService.updateFreezing(userFreezing);
        }

        UserInfo userInfo = userInfoService.findById(userId.longValue());
        userInfo.setUserStatus(2 + "");
        userInfo.setFreezingRange(freezingRange);
        userInfo.setId(userId.longValue());
        userInfoService.updateUserInfo(userId,userInfo);
    }
//解冻
    public void reasons(long userId) {
        UserInfo userInfo = userInfoService.findById(userId);
        userInfo.setUserStatus(1 + "");
        userInfoService.updateUserInfo(Integer.parseInt(userId+""),userInfo);
    }
}
